package com.yycx.module.bus.provider.handler.haiying.tasktodo.settle.invoice;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.UpdateInterceptor;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.bus.client.entity.hy.BusHyorderInoviceDetails;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.provider.service.hy.BusHyorderInoviceDetailsService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 支付个体户保存处理器
 *
 * @author zyf
 */
@Component
public class HyInvoicePaymentPayHandler implements UpdateInterceptor<BusPayment> {

    @Resource
    private BusHyorderInoviceDetailsService hyorderInoviceDetailsService;

    @Resource
    private BusPaymentService paymentService;

    @Override
    public void prepare(CriteriaUpdate cs, EntityMap entityMap, BusPayment payment) {
        Long userId = OpenHelper.getUserId();
        payment.setPayUserId(userId);
        payment.setPaymentState(CommonConstants.INT_2);
    }

    @Override
    public void complete(CriteriaUpdate cs, EntityMap params, BusPayment entity) {
        Long paymentId = entity.getPaymentId();
        entity = paymentService.getById(paymentId);
        String selectIds = entity.getSelectIds();
        if (FlymeUtils.isNotEmpty(selectIds)) {
            String[] invoiceDetailsIds = selectIds.split(",");
            for (String invoiceDetailsId : invoiceDetailsIds) {
                BusHyorderInoviceDetails busHyorderInoviceDetails = hyorderInoviceDetailsService.getById(invoiceDetailsId);
                if (FlymeUtils.isNotEmpty(busHyorderInoviceDetails)) {
                    busHyorderInoviceDetails.setSettleAmount(busHyorderInoviceDetails.getSettleApplyAmount());
                    busHyorderInoviceDetails.setSettleDate(entity.getPayDate());
                    hyorderInoviceDetailsService.updateById(busHyorderInoviceDetails);
                    //orderDetailService.updateSettleAmount(busHyorderInoviceDetails);
                }
            }
        }
    }
}
